package com.api.oa.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.api.oa.model.entity.YQTask;
import com.api.oa.model.params.YQTaskParam;
import com.api.oa.services.YQTaskService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/yq/yQTask")
public class YQTaskController extends BaseController {

    @Autowired
    private YQTaskService iYQTaskService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody YQTask entity) {
        try {
            return iYQTaskService.add(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/dispatch", method = RequestMethod.POST)
    public MyResult dispatch(@RequestBody YQTask entity) {
        try {
            return iYQTaskService.dispatchCare(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable Long id) {
        try {
            iYQTaskService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody YQTask entity) {
        try {
            return iYQTaskService.update(entity) == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * 批量申请
     * @param entity
     * @return
     */
    @RequestMapping(value = "/requestBatch", method = RequestMethod.POST)
    public MyResult requestBatch(@RequestBody YQTaskParam entity) {
        try {
            return iYQTaskService.requestBatch(entity) == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(defaultValue = "0") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) Long pid,
                         @RequestParam(required = false) String taskStats, @RequestParam(required = false) String keyWord, @RequestParam(required = false) String start, @RequestParam(required = false) String end) {
        try {
            return success(iYQTaskService.list(pid, taskStats, keyWord, start, end, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/service/req", method = RequestMethod.GET)
    public MyResult queryByInsUserId(@RequestParam(defaultValue = "0") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) Long insUserId,
                                     @RequestParam(required = false) String taskStats, @RequestParam(required = false) String keyWord, @RequestParam(required = false) String start, @RequestParam(required = false) String end) {
        try {
            return success(iYQTaskService.queryByInsUser(insUserId, taskStats, keyWord, start, end, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 护士和护工查询自己任务
     *
     * @param weekIndex
     * @param careUser
     * @return
     */
    @RequestMapping(value = "/listByWeek", method = RequestMethod.GET)
    public MyResult listByWeek(@RequestParam(defaultValue = "0") Integer weekIndex, @RequestParam(defaultValue = "0") Long careUser) {
        try {
            DateTime dateTime = DateUtil.offsetWeek(new Date(), weekIndex);
            DateTime startTime = DateUtil.beginOfWeek(dateTime);
            DateTime endTime = DateUtil.endOfWeek(dateTime);
            return success(iYQTaskService.pageWeek(startTime.toDateStr(), endTime.toDateStr(), careUser));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 护士和护工查询自己今日任务
     *
     * @param careUser
     * @return
     */
    @RequestMapping(value = "/listByToday", method = RequestMethod.GET)
    public MyResult listByToday(@RequestParam(defaultValue = "0") Long careUser, @RequestParam(defaultValue = "0") Boolean isCare, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize) {
        try {
            return success(iYQTaskService.queryByUserId(careUser, DateUtil.date().toDateStr(), isCare, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 病患查询自己的服务预约 Android
     *
     * @param careUser
     * @return
     */
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public MyResult queryServiceOrder(@RequestParam(defaultValue = "0") Long careUser, @RequestParam(defaultValue = "false") Boolean isRange, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize) {
        try {
            return success(iYQTaskService.queryTaskByPatient(careUser, isRange, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 护士和护工查询自己今日任务
     *
     * @param qrCode
     * @return
     */
    @RequestMapping(value = "/scanQR", method = RequestMethod.GET)
    public MyResult scanQR(@RequestParam(defaultValue = "") String qrCode, @RequestParam(defaultValue = "0") Long taskId, @RequestParam(defaultValue = "0") Double lat, @RequestParam(defaultValue = "0") Double lng) {
        try {
            return success(iYQTaskService.verifyCareByScanQR(qrCode, taskId, lat, lng));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null : iYQTaskService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public MyResult info(@PathVariable Long id) {
        try {
            return success(id == null ? null : iYQTaskService.info(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 任务ID 查询任务
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findOneByTaskId/{id}", method = RequestMethod.GET)
    public MyResult findOneByTaskId(@PathVariable Long id) {
        try {
            return success(id == null ? null : iYQTaskService.findOneByTaskId(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 护工查询自己剩余人数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/left/{id}", method = RequestMethod.GET)
    public MyResult left(@PathVariable Long id) {
        try {
            return success(iYQTaskService.queryLeftPatient(id, DateUtil.date().toDateStr()));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    /**
     * 护工查询自己剩余人数
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/finish/{id}", method = RequestMethod.GET)
    public MyResult finish(@PathVariable Long id) {
        try {
            return success(iYQTaskService.finishTaskById(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    //服务任务的申请
    @RequestMapping(value = "/request", method = RequestMethod.POST)
    public MyResult request(@RequestBody YQTask entity) {
        try {
            return iYQTaskService.request(entity) == 1 ? success("预约成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }

    }


    /**
     * 患者查看自己的服务  Android 满意度打分
     *
     * @return
     */
    @RequestMapping(value = "/service", method = RequestMethod.GET)
    public MyResult patientService(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Long userId) {
        try {
            return success(iYQTaskService.queryMyService(userId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }


    @RequestMapping(value = "/service/care", method = RequestMethod.GET)
    public MyResult careService(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Long userId) {
        try {
            return success(iYQTaskService.queryServiceByCare(userId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }

    /**
     * Android 满意度打分
     *
     * @return
     */
    @RequestMapping(value = "/staisfaction", method = RequestMethod.GET)
    public MyResult setStaisfaction(@RequestParam(defaultValue = "0") String taskSta, @RequestParam(defaultValue = "0") Long taskId) {
        try {
            return iYQTaskService.setTaskStaisfication(taskId, taskSta) == 1 ? success("评价成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }




    @RequestMapping(value = "/start/{taskId}", method = RequestMethod.GET)
    public MyResult setService(@PathVariable Long taskId, @RequestParam(required = true) Double lat, @RequestParam(required = true) Double lng) {
        try {
            return iYQTaskService.startTaskService(taskId,lat,lng) == 1 ? success("开始服务") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }
    }




}
